﻿using System.Globalization;
using System.Windows.Controls;

namespace SUNRPA.CONTROLS.IMPL.util {

    public class ValidationTempData { 
        private Dictionary<string, Object> validateData = new Dictionary<string, object>();
        private Dictionary<string, bool> validateRes = new Dictionary<string, bool>();
        public Dictionary<string, Object>? ValidateData { get{ return validateData;}}
        public Dictionary<string, bool>? ValidateRes { get { return validateRes;}}

        public bool GetValidationResult() {
            bool res = true;
            foreach(string key in validateRes.Keys) { 
                res = res & validateRes[key];
            }
            return res;
        }
    }

    public class BaseValidation:ValidationRule {
        private ValidationTempData tempData = null;
        public ValidationTempData? TempData{get { return tempData;} set{ tempData = value;} }
        private string tip = "";
        public string Tip { get {return tip;} set {tip = value; } }

        public void SetValRes(string tip, bool res) { 
            if(tempData.ValidateRes.TryGetValue(tip, out bool val)) { 
                tempData.ValidateRes[tip] = res;
            } else { 
                tempData.ValidateRes.Add(tip, res);
            }
        }

        public void SetValData(string tip, Object res) { 
            if(tempData.ValidateData.TryGetValue(tip, out object val)) { 
                tempData.ValidateData[tip] = res;
            } else { 
                tempData.ValidateData.Add(tip, res);
            }
        }

        public object GetValData(string tip) { 
            return tempData.ValidateData[tip];
        }

        public bool GetValRes(string tip) { 
            return tempData.ValidateRes[tip];
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
            throw new NotImplementedException();
        }
    }

    public class NotEmptyValidation:BaseValidation {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
            string valueString = value as string;
            if(string.IsNullOrEmpty(valueString)) { 
                SetValRes(Tip, false);
                return new ValidationResult(false, $"{Tip}不能为空");
            }
             SetValRes(Tip, true);
            return new ValidationResult(true, null);
        }
    }

    public class FutureDateValidationRule : BaseValidation{
        public override ValidationResult Validate(object value, CultureInfo cultureInfo){
            DateTime time;
            if (!DateTime.TryParse((value ?? "").ToString(),
                CultureInfo.CurrentCulture,
                DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces,
                out time)) return new ValidationResult(false, "Invalid date");

            return time.Date <= DateTime.Now.Date
                ? new ValidationResult(false, "Future date required")
                : ValidationResult.ValidResult;
        }
    }
}
